package com.longrise.android.byjk.plugins.course.tiku;

import android.webkit.JavascriptInterface;

import com.longrise.common.base.RxManager;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by callxkj on 2017/10/29.
 * 题库桥梁类
 */

public class TikuBridge extends BaseWebBridge {

    public TikuBridge(BaseWebActivity activity, RxManager rxManager) {
        super(activity, rxManager);
    }

    /**
     * 是否显示toolbar右边的日历按钮
     *
     * @param isShowing 1：显示日期按钮; 0：隐藏
     */
    @JavascriptInterface
    public void dateButtonStatus(final String isShowing) {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext(isShowing);
                e.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        if ("1".equals(s)) {
                            ((TikuActivity) mActivity).setRightIcon(true);
                        } else {
                            ((TikuActivity) mActivity).setRightIcon(false);
                        }
                    }
                });
        mRxManager.addDisposable(subscribe);
    }


}
